const dbModel=require('../lib/db.js')

//新建留言墙
exports.insertWall=async (req,res)=>{
    let data = req.body;
    // console.log(data);
    await dbModel.insertWall([
        data.type,
        data.message,
        data.name,
        data.userId,
        data.moment,
        data.label,
        data.color,
        data.imgurl
    ])
     .then(dbResult => {  // 修改参数名避免冲突
        res.send({
            code: 200,
            message: dbResult
        })
     })
     .catch(error => {
        console.error(error);
        res.status(500).send({
            code: 500,
            message: '服务器错误'
        });
    })
}
//新建反馈
exports.insertFeedback=async (req,res)=>{
    let data = req.body;
    // console.log(data); 
    await dbModel.insertFeedback([
        data.wallId,
        data.userId,
        data.type,
        data.moment
    ])
    .then(result => {
        res.send({
            code: 200,
            message: result
        })
     })
}
//新建评论
exports.insertComment=async (req,res)=>{
    let data = req.body;    
    console.log(data);
    await dbModel.insertComment([
        data.wallId,
        data.userId,
        data.imgurl,
        data.moment,
        data.comment,
        data.name
    ]) 
    .then(result => {
        res.send({
            code: 200,
            message:'result'
        })
     }) 
}
//删除墙，连带删除子评论
exports.deleteWall=async (req,res)=>{
    let data = req.body;
    // console.log(data);
    await dbModel.deleteWall(data.id)
    .then(result => {
        res.send({
            code: 200,
            message:'result'
        })
     })
}

//删除反馈
exports.deleteFeedback=async (req,res)=>{
    let data = req.body;
    // console.log(data);
    await dbModel.deleteFeedback(data.id)
   .then(result => {
        res.send({
            code: 200,
            message:'result'
        })
     })
}
//删除评论
exports.deleteComment=async (req,res)=>{
    let data = req.body;
    // console.log(data);
    await dbModel.deleteComment(data.id)
  .then(result => {
        res.send({
            code: 200,
            message:'result'
        })
     })
}
//分页查询墙

exports.findWallPage = async (req, res) => {
    let data = req.body;
    try {
        const result = await dbModel.findWallPage(data.page, data.pagesize, data.type, data.label);
        for (let i = 0; i < result.length; i++) {
            // 查找相应 wall 的赞、举报、撤销数据
            // 喜欢
            result[i].like = await dbModel.feedbackCount(result[i].id, 0);
            // 举报 3
            result[i].report = await dbModel.feedbackCount(result[i].id, 1);
            // 要求撤销
            result[i].revoke = await dbModel.feedbackCount(result[i].id, 2);
            // 是否点赞
            result[i].islike = await dbModel.likeCount(result[i].id, data.userId);
            // 评论数
            result[i].comcount = await dbModel.commentCount(result[i].id);
        }
        res.send({
            code: 200,
            message: result
        });
    } catch (error) {
        console.error(error);
        res.status(500).send({
            code: 500,
            message: '服务器错误'
        });
    }
};

//倒序分页查询评论
exports.findCommentPage = async (req, res) => {
    let data = req.body;
    await dbModel.findCommentPage(data.page, data.pagesize, data.id)
    .then(result => {
        res.send({
            code: 200,
            message:result
        })
     }) 
}